﻿namespace Plugins.Xml.Serialization
{
    using System;

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class XmlRuntimeSerializerAttribute : Attribute
    {
        private XmlRuntimeSerializer _serializer;
        protected XmlRuntimeSerializerAttribute(XmlRuntimeSerializer serializer)
            : base()
        {
            _serializer = serializer;
        }

        public XmlRuntimeSerializerAttribute(Type type)
            : base()
        {
            if (type == typeof(bool))
            {
                _serializer = BooleanRuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(byte))
            {
                _serializer = ByteRuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(Int16))
            {
                _serializer = Int16RuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(int))
            {
                _serializer = Int32RuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(Int64))
            {
                _serializer = Int64RuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(Decimal))
            {
                _serializer = DecimalRuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(double))
            {
                _serializer = DoubleRuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(float))
            {
                _serializer = FloatRuntimeSerializer.Instance;
                return;
            }
            if (type == typeof(string))
            {
                _serializer = StringRuntimeSerializer.Instance;
                return;
            }
            if (type.IsEnum)
            {
                _serializer = new EnumRuntimeSerializer(type);
                return;
            }
        }

        public XmlRuntimeSerializer RuntimeSerializer
        {
            get
            {
                return _serializer;
            }
        }
    }
}
